﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace GMusicBox.Core
{
    public sealed class MusicList
    {
        public MusicList()
        {
            Id = "";
            Name = "";
            Songs = new List<Song>();
        }

        public MusicList(string id, string name)
        {
            Id = id;
            Name = name;
            Songs = new List<Song>();
        }

        public string Id { get; set; }

        public string Name { get; set; }

        public List<Song> Songs { get; set; }

        public Song this[int index]
        {
            get { return Songs[index]; }
            set { Songs[index] = value; }
        }

        public void GetChartData()
        {
            try
            {
                if (Id == "") throw new ArgumentNullException("榜单Id未指定，请先设置Id属性，再执行 GetChartData 函数。");
                XmlDocument xd = new XmlDocument(); xd.Load("http://www.google.cn/music/chartlisting?q=" + Id + "&cat=song&output=xml");
                Name = xd.SelectSingleNode("results/playlist/info/name").InnerText;
                Songs = new List<Song>(); Songs.AddRange(Song.GetSongList(xd, "results/playlist/songList/song"));
            }
            catch { }
        }

        public void GetTopiclistingData()
        {
            try
            {
                if (Id == "") throw new ArgumentNullException("音乐专题Id未指定，请先设置Id属性，再执行 GetTopiclistingData 函数。");
                XmlDocument xd = new XmlDocument(); xd.Load("http://www.google.cn/music/topiclisting?q=" + Id + "&cat=song&output=xml");
                Name = xd.SelectSingleNode("results/playlist/info/name").InnerText;
                Songs = new List<Song>(); Songs.AddRange(Song.GetSongList(xd, "results/playlist/songList/song"));
            }
            catch { }
        }

        public static MusicList[] GetTopiclistings()
        {
            try
            {
                XmlDocument xd = new XmlDocument(); xd.Load("http://www.google.cn/music/topiclistingdir?cat=song&output=xml");
                XmlNodeList xn = xd.SelectNodes("results/directory/node");
                List<MusicList> mls = new List<MusicList>();
                for (int i = 0; i < xn.Count; i++)
                {
                    XmlNode id = xn[i].SelectSingleNode("id");
                    MusicList ml = new MusicList(id.InnerText, "");
                    ml.GetTopiclistingData();
                    mls.Add(ml);
                }
                return mls.ToArray();
            }
            catch { }

            return null;
        }

        public void Load(string fileName)
        {
            XmlDocument xd = new XmlDocument(); xd.Load(fileName);
            XmlNode xn = xd.SelectSingleNode("results");
            Name = xn.SelectSingleNode("listname").InnerText;
            Songs = new List<Song>(); Songs.AddRange(GMusicBox.Core.Song.GetSongList(xd, "results/songlist/song"));
        }

        public void Save(string fileName)
        {
            string body = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\r\n<results>\r\n\t<listname>" + Name + "</listname>\r\n\t<songlist>\r\n";
            for (int i = 0; i < Songs.Count; i++)
                body += Songs[i].ToXML(2);

            body += "\t</songlist>\r\n</results>";

            File.WriteAllText(fileName, body);
        }

        public override string ToString()
        {
            return Name;
        }
    }
}
